{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# scikit-learn → PMML\n",
    "\n",
    "\n",
    "### Exporter: Random Forest\n",
    "### Data Set used: Iris\n",
    "\n",
    "\n",
    "### **STEPS**:\n",
    "- Build the Pipeline with preprocessing (using DataFrameMapper)\n",
    "- Build PMML using Nyoka exporter"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Pre-processing, Model building (using pipeline) for Iris data set"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-13T17:24:07.796366Z",
     "start_time": "2018-08-13T17:24:06.627366Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:\\Users\\nibo\\anaconda\\lib\\site-packages\\sklearn\\ensemble\\weight_boosting.py:29: DeprecationWarning: numpy.core.umath_tests is an internal NumPy module and should not be imported. It will be removed in a future NumPy release.\n",
      "  from numpy.core.umath_tests import inner1d\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "Pipeline(memory=None,\n",
       "     steps=[('mapping', DataFrameMapper(default=False, df_out=False,\n",
       "        features=[(['sepal length (cm)', 'sepal width (cm)'], StandardScaler(copy=True, with_mean=True, with_std=True)), (['petal length (cm)', 'petal width (cm)'], Imputer(axis=0, copy=True, missing_values='NaN', strategy='mean', verbo...n_jobs=1,\n",
       "            oob_score=False, random_state=None, verbose=0,\n",
       "            warm_start=False))])"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import pandas as pd\n",
    "from sklearn import datasets\n",
    "from sklearn.pipeline import Pipeline\n",
    "from sklearn.preprocessing import StandardScaler, Imputer\n",
    "from sklearn_pandas import DataFrameMapper\n",
    "from sklearn.ensemble import RandomForestClassifier\n",
    "\n",
    "iris = datasets.load_iris()\n",
    "irisd = pd.DataFrame(iris.data,columns=iris.feature_names)\n",
    "irisd['Species'] = iris.target\n",
    "\n",
    "features = irisd.columns.drop('Species')\n",
    "target = 'Species'\n",
    "\n",
    "pipeline_obj = Pipeline([\n",
    "    (\"mapping\", DataFrameMapper([\n",
    "    (['sepal length (cm)', 'sepal width (cm)'], StandardScaler()) , \n",
    "    (['petal length (cm)', 'petal width (cm)'], Imputer())\n",
    "    ])),\n",
    "    (\"rfc\", RandomForestClassifier(n_estimators = 100))\n",
    "])\n",
    "\n",
    "pipeline_obj.fit(irisd[features],irisd[target])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Export the Pipeline object into PMML using the Nyoka package"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-08-13T17:24:19.024866Z",
     "start_time": "2018-08-13T17:24:18.196366Z"
    }
   },
   "outputs": [],
   "source": [
    "from nyoka import skl_to_pmml\n",
    "\n",
    "skl_to_pmml(pipeline_obj,features,target,\"rf_pmml.pmml\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  },
  "latex_envs": {
   "LaTeX_envs_menu_present": true,
   "autoclose": false,
   "autocomplete": true,
   "bibliofile": "biblio.bib",
   "cite_by": "apalike",
   "current_citInitial": 1,
   "eqLabelWithNumbers": true,
   "eqNumInitial": 1,
   "hotkeys": {
    "equation": "Ctrl-E",
    "itemize": "Ctrl-I"
   },
   "labels_anchors": false,
   "latex_user_defs": false,
   "report_style_numbering": false,
   "user_envs_cfg": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
